/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */
package service.impl;

import java.util.List;
import pojo.Thread;
import service.IThreadService;

/**
 *
 * @author administrator
 */
public class ThreadServiceImpl<T extends Thread> extends ServiceImpl<T>
        implements IThreadService<T> {

    public List<T> listThread(int categoryID, int offset, int pageSize) {
        return this.getDao().list("select t from Thread t "
                + " where t.top = 0 and t.category.categoryId = " + categoryID + " order by t.postDate desc",
                offset, pageSize, null);
    }

    public List<T> listRecommend(int categoryID,int offset,int pageSize)
    {
        return this.getDao().list("select t from Thread t "
                + " where t.digest = 1 and t.category.categoryId = " + categoryID + " order by t.postDate desc",
                offset, pageSize, null);
    }

    public List<T> listTop(int categoryID,int offset,int pageSize)
    {
        return this.getDao().list("select t from Thread t "
                + " where t.top = 1 and t.category.categoryId = " + categoryID + " order by t.postDate desc",
                offset, pageSize, null);
    }

    public List<T> listHistory(String uni,int offset,int pageSize)
    {
        return this.getDao().list("select t from Thread t "
                + " where t.user.unikey = " + "'" + uni + "'" + " order by t.postDate desc",
                offset, pageSize, null);
    }
    //
    public List<T> findThread(String toFind, int offset, int pageSize) {
        return this.getDao().list("select t from Thread t "
                + " where t.threadTitle like '%" + toFind+"%' order by t.postDate desc",
                offset, pageSize, null);
    }

    public int count(int categoryID) {
        return this.getTotalCount("select count(t) from Thread t "
                + " where t.category.categoryId = " + categoryID, null);
    }

    public int count(String toFind) {
        return this.getTotalCount("select count(t) from Thread t "
                + "where t.threadTitle like '%" + toFind+"%' ", null);
    }

    public int countRecommend(int categoryID)
    {
        return this.getTotalCount("select count(t) from Thread t "
                + " where t.digest = 1 and t.category.categoryId = " + categoryID, null);
    }

    public int countHistory(String uni)
    {
        return this.getTotalCount("select count(t) from Thread t "
                + " where t.user.unikey = " + "'" + uni + "'", null);
    }
    public T GetThread(int threadID) {
        List<T> thread = this.getDao().createQuery(
                " select t from Thread t "
                + " where t.threadId = :ID ").setParameter("ID", threadID).list();

        if (thread.size() > 0) {
            return thread.get(0);
        }
        return null;
    }
    //get thread by name

    public T GetThread(String name) {
        List<T> thread = this.getDao().createQuery(
                " select t from Thread t "
                + " where t.threadId = :ID ").setParameter("ThreadTitle", name).list();

        if (thread.size() > 0) {
            return thread.get(0);
        }
        return null;
    }

    public T GetLastThread(int categoryID)
    {
         List<T> thread = this.getDao().createQuery(
                " select t from Thread t "
                + " where t.category.categoryId = " + categoryID + " order by t.postDate desc limit 1").list();

        if (thread.size() > 0) {
            return thread.get(0);
        }
        return null;
    }

    public void IncreaseViewTimes(T thread) {
        thread.setViewTimes(thread.getViewTimes() + 1);
        this.getDao().save(thread);
    }

    public void IncreaseReplyTimes(T thread)
    {
        thread.setReplyTimes(thread.getReplyTimes() + 1);
        this.getDao().save(thread);
    }


    /**
     * @author: Yu
     * @return
     * I use this method to get the newThread's ID
     */
    //public int countTotalNumberOfThread(){
        //return this.countAll(" select count(t) from Thread t ");
    //}


    public void createThread(T thread){
        this.getDao().save(thread);
    }

    public void deleteThread(T thread)
    {
        this.getDao().delete(thread);
    }

    public void updateThread(T thread)
    {
        this.getDao().save(thread);
    }

     public int countName(String toFind) {
        return this.getTotalCount("select count(t) from Thread t "
                + "where t.ThreadTitle like "+'"'+"%" + toFind+"%"+'"', null);
    }

}
